let router = require('../router/Router');
let authService = require('../service/AuthService');

const { BusiError, SysError } = require('../exception/exception');

router.post('/auth/authcode/get', (req, resp) => {
    authService.getAuthCode(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        let msg = "系统异常, 请稍后再试";
        if (err instanceof BusiError){
            msg = err.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

router.post('/gateway/auth/authcode/valid', (req, resp) => {
    if(!req.body.uid){
        resp.send({code: 500, msg: "uid不能为空"}); 
        return;
    }

    if(!req.body.code){
        resp.send({code: 500, msg: "code不能为空"}); 
        return;
    }

    authService.validAuthCode(req.body.uid, req.body.code).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        let msg = "系统异常, 请稍后再试";
        if (err instanceof BusiError){
            msg = err.message;
        }
        resp.send({code: 500, msg: msg});
    });
});